// pages/suohuodz/suohuodz.js
const app = getApp()
var ea = require('../../utils/each.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    checked: '',
    getAddressUrl: app.data.url + '/api/my/getaddress',
    delAddressUrel: app.data.url + '/api/my/deladdress',
    setAddressUrl: app.data.url + '/api/my/setmoren',
    shuju: [],
    morenaddressid:0,
  },
  // 编辑跳转到--编辑收货地址页面
  xiugai: function (e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/xiugai/xiugai?id=' + id,
    })
  },
  // 地址删除
  shanchu: function (e) {
    var id = e.currentTarget.dataset.id;
    var that = this;
    // console.log(id);
    var user = wx.getStorageSync('user')
    wx.showModal({
      title: '提示',
      content: '确认删除收货地址',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: that.data.delAddressUrel,
            method: 'post',
            data: { token: user.token, id: id },
            success: function (res) {
              // console.log(res);
              var data = res.data;
              if (data.code == 200) {
                wx.showToast({
                  title: '删除地址成功',
                  icon: 'success',
                  duration: 2000
                })
                var shuju = that.data.shuju;
                var newshuju = [];
                ea.each(shuju, function (i, v) {
                  if (v.id !== id) {
                    newshuju.push(v);
                  }
                })
                that.setData({ shuju: newshuju })
              } else {
                wx.showToast({
                  title: data.msg,
                  icon: 'none'
                })
              }
            }
          })
        }
      }
    })
  },

  qiehuan: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var shuju = this.data.shuju;
    var newshuju = [];
    wx.setStorageSync('nowaddress', id);
    ea.each(shuju, function (i, v) {
      if (v.id == id) {
        v.moren = 1;
      } else {
        v.moren = 0;
      }
      newshuju.push(v);
    });
    that.setData({shuju:newshuju});
  },


  tianjia: function () {
    // wx.navigateTo({
    //   url: '/pages/bianjidizhi/bianjidizhi',
    // })
    wx.navigateBack({
      delta:1
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var that = this;
    var id = options.id;
    that.setData({ morenaddressid:id});
    var user = wx.getStorageSync('user');
    wx.showLoading({
      title: '加载中',
    })
    //console.log(user);
    wx.request({
      url: that.data.getAddressUrl,
      data: { token: user.token },
      success: function (res) {
        var data = res.data;
        //console.log(data);
        if (data.code == 200) {
          var shuju = data.data.address;
          var newshuju = [];
          console.log(shuju)
          ea.each(shuju,function(i,v){
            if(v.id==id){
              v.moren = 1;
            }else{
              v.moren = 0;
            }
            newshuju.push(v);
          })
          that.setData({ shuju: newshuju });
        } else {
          wx.showToast({
            title: data.msg,
            icon: 'none'
          })
        }
        wx.hideLoading();
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var id = this.data.morenaddressid;
    this.onLoad({id:id});
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var id = this.data.morenaddressid;
      this.onLoad({id:id});
    wx.stopPullDownRefresh({
      success: function () {
        console.log('返回了')
      }
    });
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var user = wx.getStorageSync('user');
    var titles = wx.getStorageSync('titles');
    return {
      title: titles,
      path: '/pages/index/index?scene' + user.id,
    };
  }
})